/**
 * Author : duchuynh
 * Nov 9, 2011
 */
package wte.rnd.buildingontology.gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Stack;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.Document;
import javax.swing.text.StyledDocument;
import javax.swing.text.html.HTMLDocument;

import org.apache.axis.handlers.DebugHandler;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

import wte.rnd.buildingontology.core.StringProcessing;
import wte.rnd.buildingontology.utilities.SaxParser;
import wte.rnd.buildingontology.core.EntityInfo;

public class SimpleBrowserPanel extends JPanel{

	/**
	 * Nov 9, 2011
	 * Author : duchuynh
	 * @param args
	 */
	private JTextField urlJTextField;
	private JButton backJButton;
	private JButton loadJButton;
	private JEditorPane editorPane;
	private Stack urlStack = new Stack();
	private ArrayList<String> listEntityFromURL;
	
	public SimpleBrowserPanel(){
		super(new BorderLayout());		
		urlJTextField = new JTextField();
		urlJTextField.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
				if(e.getKeyCode() == KeyEvent.VK_ENTER){
					try {
						urlStack.push("http://" + urlJTextField.getText());						
						editorPane.setPage("http://" + urlJTextField.getText());
						MainFrame.listEnableEntities = StringProcessing.findEntityFormURL(editorPane.getPage());
						MainFrame.repaintCatchListEntitites();
					} catch (Exception e1) {
						JOptionPane.showMessageDialog(null, e1.getMessage());
					}
					
				}
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		urlJTextField.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent arg0) {
				
			}
		});
		editorPane = new JEditorPane();
    
	    Font font = UIManager.getFont("Label.font");	  
	    editorPane.setOpaque(false);
	    editorPane.setBorder(null);
	    editorPane.setEditable(false);
	    editorPane.setFont( new Font( "Dialog", Font.BOLD, 10 ) );
	   
		backJButton = new JButton("Back");
		
		loadJButton = new JButton("Go");
		loadJButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					urlStack.push("http://" + urlJTextField.getText());
					editorPane.setPage("http://" + urlJTextField.getText());
					MainFrame.listEnableEntities = StringProcessing.findEntityFormURL(editorPane.getPage());
					MainFrame.repaintCatchListEntitites();
				} catch (Exception e) {
					JOptionPane.showMessageDialog(null, e.getMessage());
				}
				
			}
		});		
		backJButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(urlStack.size() <= 1) return;
				try {
					urlStack.pop();
					String url = (String)urlStack.peek();
					editorPane.setPage(url);
					urlJTextField.setText(url);
					MainFrame.listEnableEntities = StringProcessing.findEntityFormURL(editorPane.getPage());
					MainFrame.repaintCatchListEntitites();
				} catch (Exception e2) {
					JOptionPane.showMessageDialog(null, e2.getMessage());
				}
			}
		});
		editorPane.setEditable(false);
		editorPane.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				if(e.getButton() == MouseEvent.BUTTON1 && editorPane.getSelectedText() != null){
					EntityInfo en = new EntityInfo();
					en.setText(editorPane.getSelectedText());
					MainFrame.listExistingEntities.add(en);
					MainFrame.repaintExistingListEntitites();
				}
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		editorPane.addHyperlinkListener(new HyperlinkListener() {
			
			@Override
			public void hyperlinkUpdate(HyperlinkEvent e) {
				if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED){
					try {
						urlStack.push(e.getURL().toString());
						editorPane.setPage(e.getURL().toString());
						urlJTextField.setText(e.getURL().toString());
						MainFrame.listEnableEntities = StringProcessing.findEntityFormURL(editorPane.getPage());
						MainFrame.repaintCatchListEntitites();
					} catch (Exception e3) {
						JOptionPane.showMessageDialog(null, e3.getMessage());
					}
				}				
			}
		});		
		
		JPanel topPanel = new JPanel(new BorderLayout());
		topPanel.add(backJButton, BorderLayout.WEST);
		topPanel.add(urlJTextField, BorderLayout.CENTER);
		topPanel.add(loadJButton, BorderLayout.EAST);
		JScrollPane viewPort = new JScrollPane(editorPane);
		viewPort.getViewport().setViewPosition(new Point(0, 0));
		this.add(topPanel, BorderLayout.NORTH);		
		this.add(viewPort, BorderLayout.CENTER);			
	}
	
	public ArrayList<String> getListEntityFromURL() {
		return listEntityFromURL;
	}

	public void setListEntityFromURL(ArrayList<String> listEntityFromURL) {
		this.listEntityFromURL = listEntityFromURL;
	}

	public static void main(String[] args) {
		JFrame fr = new JFrame("demo");
		SimpleBrowserPanel browser = new SimpleBrowserPanel();
		Container container = fr.getContentPane();
		JPanel contain = new JPanel(new GridLayout(2, 0));
		contain.add(browser);		
		//contain.add(textEntity);
		container.add(contain);
		
		fr.setEnabled(true);
		fr.setVisible(true);
		fr.setSize(800, 800);
		fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
}

